package com.ddn.controller.logistics;

import com.alibaba.excel.EasyExcel;

import com.ddn.mapper.logistics.dto.HqPurchasingExportyDto;
import com.ddn.model.logistics.HqPurchasing;
import com.ddn.service.logistics.HqPurchasingService;
import com.ddn.vo.PageBean;
import com.ddn.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author zlp
 * @date 2022/8/28 13:51
 */
@RestController
@RequestMapping("/api/hqpurchasing/")
public class HqPurchasingController {
    @Autowired
    private HqPurchasingService service;


    //新增
    @PostMapping("save.do")
    public R save(HqPurchasing purchasing){
        return service.save(purchasing);
    }
    //更新
    @PostMapping("update.do")
    public R update(HqPurchasing purchasing){
        return service.update(purchasing);
    }
    //删除
    @GetMapping("del.do")
    public R del(int id){
        return service.delete(id);
    }
    //查询
    @GetMapping("page.do")
    public PageBean page(String name, int page, int limit){
        return service.pageByName(name,page,limit);
    }
    //连表查询
    @GetMapping("unpage.do")
    public PageBean unpage(String name, int page, int limit){
        System.out.println("****controller"+name);
        return service.unByName(name,page,limit);
    }
//导出数据
    @GetMapping("download.do")
    public void download(HttpServletResponse response) throws IOException {
        //1设置消息响应头
        response.setHeader("content-disposition","attachment;filename=purchasing"+System.currentTimeMillis()+".xlsx");
        //2.查询数据库中的数据
        List<HqPurchasingExportyDto> list=service.all();
        //3.实现数据导出Excel格式
        EasyExcel.write(response.getOutputStream(),HqPurchasingExportyDto.class).sheet("Purchasing").doWrite(list);
    }
//导入数据
    @PostMapping("upload.do")
    public R upload(MultipartFile file) throws IOException {
        return service.upload(file);
    }
    /**
     * 批量删除
     */
    //删除
    @PostMapping("delbyids.do")
    public R delByIds(int[] ids){
        return service.delByIds(ids);
    }

}
